package com.leyou.user.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.mapper.AddressMapper;
import com.leyou.user.pojo.Address;
import com.leyou.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/5/20 0020
 * Time: 14:28
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public List<AddressDTO> queryAddresses(Long uid) {
        Address orderAddress = new Address();
        orderAddress.setUid(uid);
        List<Address> list = addressMapper.select(orderAddress);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.USER_ADDRESS_NOT);
        }
        return BeanHelper.copyWithCollection(list, AddressDTO.class);
    }

    @Override
    public AddressDTO queryAddressByUidAndId(Long uid, Long id) {
        Address newAddress = new Address();
        newAddress.setId(id);
        newAddress.setUid(uid);
        Address address = addressMapper.selectOne(newAddress);
        if (address == null) {
            throw new LyException(ExceptionEnum.ADDRESS_NOT_FOUND);
        }
        return BeanHelper.copyProperties(address, AddressDTO.class);
    }
}
